<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <meta http-equiv="X-UA-Compatible" content="IE=edge">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>Document</title>
</head>
<body>
    <script>

        // Promise.race() 接收一个promise数组
        // 只要p1, p2, p3之中 有一个实例率先改变状态，p的状态就跟着改变
        // race返回一个promise，它可以是完成态（成功态fulfilled），也可以是失败（rejected）

        // race 竞赛，只认第一名

        // const sleep = (ms) => {
        //     return new Promise((resolve) =>{
        //         setTimeout(() => {
        //             resolve(ms)
        //         }, ms);
        //     })
        // }

        // const sleep1 = s => new Promise(resolve => setTimeout(resolve, s * 1000))
        // 上面这个返回值是undefined，1s后 => 成功态

        const sleep2 = s => new Promise(resolve => setTimeout(resolve(s), s * 1000))
        console.log(sleep2(1000))

        // const sleep = s => new Promise(resolve => setTimeout(() => { resolve(s) }, s * 1000))
        // sleep(1000).then(res => {
        //     console.log(res)
        //     console.log(222)
        // })
        // sleep(1000).then(res => {
        //     console.log(res)
        // })

        const p1 = sleep(1)
        const p2 = sleep(6)
        const p3 = sleep(5)
        const p4 = Promise.reject('error')

        const p = Promise.race([p1, p2, p3, p4])
        console.log(p)
        p.then(res => {
            console.log(res)
        }).catch(err => {
            console.log(err)
        })
    </script>
</body>
</html>